<template>
  <div>
    <div class="advertisement-info">
      <div class="content">
        <div class="project-info">
          <div class="advertisement-form mt50">
            <el-form
              style="max-width: 1200px"
              :model="state.form"
              ref="formRef"
              :rules="rules"
              label-position="top"
            >
              <el-form-item label="名称" prop="mingcheng">
                <el-col :span="10">
                  <el-input size="large" v-model="state.form.mingcheng" />
                </el-col>
              </el-form-item>
              <el-form-item label="视频/图片类型" prop="vrleixing">
                <el-select
                  width="240"
                  show-border
                  v-model="state.form.vrleixing"
                  @change="select"
                  size="large"
                  placeholder="VR类型"
                >
                  <el-option
                    v-for="item in state.vrType"
                    :key="item.dicValue"
                    :label="item.dicLabel"
                    :value="item.dicValue"
                  >
                    {{ item.dicLabel }}
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                v-if="state.form.vrleixing === 'vrleixing_tupian'"
                label="上传图片(建议尺寸:560*1160px)"
                prop="zhantingneirong"
              >
                <bjz-oss
                  :previewList="state.form.zhantingneirong"
                  :accept="['.jpg', '.png', '.jpeg']"
                  @success="successImg"
                  @remove="removeImg"
                />
              </el-form-item>
              <el-form-item
                v-if="state.form.vrleixing === 'vrleixing_shipin'"
                label="上传视频"
                prop="zhantingneirong"
              >
                <bjz-oss
                  :previewList="state.form.zhantingneirong"
                  :accept="['.mp4']"
                  :size="100"
                  @success="successImg"
                  @remove="removeImg"
                />
              </el-form-item>
              <el-form-item label="展厅介绍" prop="zhantingjieshao">
                <el-input
                  type="textarea"
                  rows="8"
                  size="large"
                  v-model="state.form.zhantingjieshao"
                />
              </el-form-item>
              <el-form-item
                v-if="state.form.vrleixing == 'vrleixing_tupian'"
                label="类型"
                prop="leixing"
              >
                <el-select
                  width="240"
                  show-border
                  v-model="state.form.leixing"
                  @change="select"
                  size="large"
                  placeholder="类型"
                >
                  <el-option
                    v-for="item in state.type"
                    :key="item.dicValue"
                    :label="item.dicLabel"
                    :value="item.dicValue"
                  >
                    {{ item.dicLabel }}
                  </el-option>
                </el-select>
              </el-form-item>
              <el-form-item
                v-if="
                  state.form.leixing == 'vrzhantingleixing_lianjie' &&
                  state.form.vrleixing == 'vrleixing_tupian'
                "
                prop="zhantingleixing"
                label="链接"
              >
                <el-col :span="10">
                  <el-input size="large" v-model="state.form.zhantingleixing" />
                </el-col>
              </el-form-item>
              <el-form-item
                v-if="
                  state.form.leixing == 'vrzhantingleixing_fuwenben' &&
                  state.form.vrleixing == 'vrleixing_tupian'
                "
                label="展厅介绍"
                prop="zhantingleixing"
              >
                <editor
                  :initValue="state.form.zhantingleixing"
                  :index="index"
                  @onVal="onVal"
                />
              </el-form-item>
              <el-form-item label="状态">
                <el-radio-group v-model="state.form.shangjia">
                  <el-radio :label="1">上架</el-radio>
                  <el-radio :label="0">下架</el-radio>
                </el-radio-group>
              </el-form-item>
              <el-form-item class="button-group">
                <el-button
                  style="margin-left: 100px"
                  type="primary"
                  @click="submitForm(formRef)"
                  >提交</el-button
                >
                <el-button
                  type="primary"
                  style="margin-left: 50px"
                  @click="$router.go(-1)"
                  >取消</el-button
                >
              </el-form-item>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>
<script setup>
import { reactive, ref } from "vue";
import { useRoute, useRouter } from "vue-router";
import { GetDictionaryTree } from "@/api/system";
import { baseMsg } from "@/utils/utils";
import { AddVR, EditVR, GetVR } from "@/api/vr";
const route = useRoute();
const router = useRouter();
const formRef = ref(null);
const validateImg = (rule, value, callback) => {
  if (state.form.zhantingneirong && state.form.zhantingneirong.length) {
    callback();
  } else {
    callback(new Error("请上传展厅（视频/图片）"));
  }
};
const rules = {
  mingcheng: [{ required: true, message: "请输入展厅名称", trigger: "blur" }],
  vrleixing: [{ required: true, message: "请选择展厅类型", trigger: "change" }],
  zhantingjieshao: [
    { required: true, message: "请输入展厅介绍", trigger: "blur" },
  ],
  leixing: [{ required: true, message: "请选择跳转类型", trigger: "change" }],
  zhantingneirong: [
    { required: true, trigger: "blur", validator: validateImg },
  ],
  zhantingleixing: [
    { required: true, message: "请输入展厅介绍", trigger: "blur" },
  ],
};
const state = reactive({
  form: {
    mingcheng: "",
    zhantingneirong: [],
    leixing: "",
    zhantingjieshao: "",
    shangjia: 0,
    zhantingleixing: "",
    vrleixing: "",
  },
  type: [],
  vrType: [],
});
const html2text = (html) => {
  html = html.replace(/(\n)/g, "");
  html = html.replace(/(\t)/g, "");
  html = html.replace(/(\r)/g, "");
  html = html.replace(/<\/?[^>]*>/g, "");
  html = html.replace(/(\s)/g, "");
  return html;
};
// 查询展厅内容类型下拉
const getVRType = () => {
  GetDictionaryTree({ dicModel: "vrzhantingleixing" }).then((res) => {
    if (res.code == 4201) {
      state.type = res.data;
    }
  });
};
// 查询VR类型下拉
const getVRTypeSelection = () => {
  GetDictionaryTree({ dicModel: "vrleixing" }).then((res) => {
    if (res.code == 4201) {
      state.vrType = res.data;
    }
  });
};
// 查询VR信息
const GetInfo = () => {
  route.query.type === "edit" &&
    GetVR({ id: route.query.id }).then((res) => {
      if (res.code == 4201) {
        state.form = res.data;
      }
    });
};
// 图片上传成功
const successImg = (res) => {
  state.form.zhantingneirong.push(res);
};
// 上传文件删除
const removeImg = (index) => {
  state.form.zhantingneirong.splice(index, 1);
};
const select = (val) => {
  if (val == "vrzhantingleixing_lianjie") {
    state.form.zhantingleixing = html2text(state.form.zhantingleixing);
  }
};
// 富文本编辑器
const onVal = (val) => {
  state.form.zhantingleixing = val;
};
// 提交表单
const submitForm = async (formEl) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      if (route.query.type == "add") {
        AddVR(state.form).then((res) => {
          if (res.code == 4202) {
            baseMsg("新增VR展厅成功");
            router.go(-1);
          }
        });
      }
      if (route.query.type == "edit") {
        EditVR(state.form).then((res) => {
          if (res.code == 4202) {
            baseMsg("编辑VR展厅成功");
            router.go(-1);
          }
        });
      }
    }
  });
};
GetInfo();
getVRType();
getVRTypeSelection();
</script>
<style lang="scss" scoped>
.content {
  border: none !important;
}
.project-info {
  padding: 30px 100px;
  font-size: 20px;
  color: black;
  display: flex;
  .advertisement-form {
    width: 1200px;
  }
}
.el-form-item {
  display: flex;
  flex-direction: column;
  &.vue-editor {
    align-items: baseline;
  }
  &.status {
    flex-direction: row;
  }
}
</style>
<style lang="scss">
.advertisement-form {
  textarea {
    &[name="editorValue"] {
      display: none;
    }
  }
}
.button-group {
  margin-top: 50px;
  .el-form-item__content {
    width: 100% !important;
    justify-content: center;
  }
}
</style>
